const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        deptId: null,
        deptName: "",
        postId: null,
        postName: "",
        form: {
            userId: null,
            userName: '',
            realName: '',
            password: '',
            phone: '',
            sex: '0',
            deptId: null,
            postId: null
        },
        resourceIds: [],

        items: [
            {value: '0', name: '男', checked: 'true'},
            {value: '1', name: '女'},
            {value: '2', name: '保密'},
        ],

        type: 0,
        showModalStatus: false,
        dataList: []
    },

    onLoad: function (options) {
        let self = this;
        self.getDeptListAll("");


        if (options.userId) {
            wx.setNavigationBarTitle({
                title: '修改员工'
            })

            app.fetchApis(self, qCloud.api.getSysUserInfo, {userId: options.userId}, "GET", function (e) {
                if (e.data.code == 200) {
                    self.setData({
                        form: e.data.data,
                        deptId: e.data.data.deptId,
                        deptName: e.data.data.deptNameStr,
                        postId: e.data.data.postId,
                        postName: e.data.data.postName
                    })
                }
            })
        } else {
            wx.setNavigationBarTitle({
                title: '添加员工'
            })

            app.fetchApis(self, qCloud.api.getEnterpriseOrganId, {}, "GET", function (e) {
                if (e.data.code == 200) {
                    self.setData({
                        resourceIds: e.data.data
                    })
                }
            })
        }
    },

    realNameInput: function (e) {
        this.data.form.realName = e.detail.value;
    },

    phoneInput: function (e) {
        this.data.form.phone = e.detail.value;
    },

    sexRadioChange: function (e) {
        this.data.form.sex = e.detail.value
    },

    userNameInput: function (e) {
        this.data.form.userName = e.detail.value
    },

    passwordInput: function (e) {
        this.data.form.password = e.detail.value
    },

    chooseDeptAndPost: function (e) {
        let type = e.currentTarget.dataset.type;
        this.setData({
            type: type
        })

        if (type == 0) {
            this.getDeptListAll("")
        } else {
            this.getPostListAll("")
        }

        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });

        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
            showModalStatus: true
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export()
            });
        }.bind(this), 200);
    },

    hideShowModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                showModalStatus: false
            })
        }.bind(this), 200);

        this.setData({
            dataList: [],
            type: 0
        })
    },

    searchInput: function (e) {
        let self = this;
        if (self.data.type == 0) {
            self.getDeptListAll(e.detail.value)
        } else {
            self.getPostListAll(e.detail.value)
        }
    },

    confirmChoose: function (e) {
        let index = e.currentTarget.dataset.index;
        if (this.data.type == 0) {
            this.setData({
                deptId: this.data.dataList[index].id,
                deptName: this.data.dataList[index].name,
                showModalStatus: false
            })
        } else {
            this.setData({
                postId: this.data.dataList[index].id,
                postName: this.data.dataList[index].name,
                showModalStatus: false
            })
        }
    },

    getDeptListAll: function (value) {
        let self = this;
        let params = {
            deptId: qCloud.session.getUser().organId,
            deptName: value
        };

        app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
            if (e.data.code == 200) {
                let dataList = [];
                for (let i = 0; i < e.data.data.length; i++) {
                    let dept = e.data.data[i];
                    dataList.push({
                        id: dept.deptId,
                        name: dept.deptNameStr
                    })
                }
                self.setData({
                    dataList: dataList
                })
            }
        })
    },

    /** 获取岗位信息*/
    getPostListAll: function (value) {
        let self = this;
        let params = {
            postName: value
        }
        app.fetchApis(self, qCloud.api.getMyPostList, params, "GET", function (e) {
            if (e.data.code == 200) {
                let dataList = [];
                for (let i = 0; i < e.data.data.length; i++) {
                    let post = e.data.data[i];
                    dataList.push({
                        id: post.postId,
                        name: post.postName
                    })
                }
                self.setData({
                    dataList: dataList
                })
            }
        })
    },

    /**
     * 保存岗位信息
     */
    addUser: function () {
        let self = this;
        if (self.data.form.realName == "") {
            wx.showToast({
                title: '员工姓名不能为空',
                icon: "error"
            })
            return;
        }

        if (self.data.form.phone == "") {
            wx.showToast({
                title: '手机号码不能为空',
                icon: "error"
            })
            return;
        }

        if (self.data.form.userName == "") {
            wx.showToast({
                title: '登录账号不能为空',
                icon: "error"
            })
            return;
        }

        if (self.data.form.password == "") {
            wx.showToast({
                title: '登录密码不能为空',
                icon: "error"
            })
            return;
        }

        let userInfo = qCloud.session.getUser();

        let params = {
            userId: null,
            organId: userInfo.organId,
            deptId: self.data.deptId,
            ancestors: null,
            userName: self.data.form.userName,
            nickName: null,
            realName: self.data.form.realName,
            password: qCloud.crypto.encrypt(self.data.form.password),
            phone: self.data.form.phone,
            email: null,
            sex: self.data.form.sex,
            leader: 'N',
            status: '0',
            kindType: '4',
            remark: null,
            postId: self.data.postId,
            manageRoleIds: [],
            clientRoleIds: [],
            scopeIds: ['00', '01'],
            resourceIds: self.data.resourceIds
        }

        app.fetchApis(self, qCloud.api.addUser, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: e.data.msg,
                    icon: "error"
                })
            }
        })
    },

    updateUser: function () {
        let self = this;
        if (self.data.form.realName == "") {
            wx.showToast({
                title: '员工姓名不能为空',
                icon: "error"
            })
            return;
        }

        if (self.data.form.phone == "") {
            wx.showToast({
                title: '手机号码不能为空',
                icon: "error"
            })
            return;
        }

        if (self.data.form.userName == "") {
            wx.showToast({
                title: '登录账号不能为空',
                icon: "error"
            })
            return;
        }

        let params = {
            userId: self.data.form.userId,
            organId: self.data.form.organId,
            deptId: self.data.deptId,
            ancestors: null,
            userName: self.data.form.userName,
            nickName: null,
            realName: self.data.form.realName,
            password: null,
            phone: self.data.form.phone,
            email: null,
            sex: self.data.form.sex,
            leader: 'N',
            status: '0',
            kindType: '4',
            postId:  self.data.postId,
            remark: null,
            manageRoleIds: [],
            clientRoleIds: [],
            scopeIds: ['00', '01'],
            resourceIds: self.data.resourceIds
        }

        app.fetchApis(self, qCloud.api.editUser, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: e.data.msg,
                    icon: "error"
                })
            }
        })
    }

})
